$(function () {
    obj.init();
});
var obj =new function () {
    'use strict';
    return {
        form:null,
        demoModel:null,
        existPhone: [],
        init:function () {
            this.demoModel = $('.modelTr').eq(0).clone();
            $('[name=iReturnId],[name="prods[0][iProdId]"]').select2();
            this.form = new xhForm($('#xh_form'));
            var  time = new Date().format('yyyy-MM-dd');
            $('[name=tApplyDate]').val(time);
            this.listener();
        },
        listener:function () {
            var  $this = this;

            //添加商品
            $('#addProd').click(function () {
                $this.funObj().addProdAction();
            });

            //提交按钮
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().submitForm();
            });
            //修改物品，检查是否是手机
            $('#xh_form').on('change','[id^="prodId_"]',function () {
                var id = $(this).attr("id").split("prodId_")[1];//第几行
                $this.funObj().changeProd(id);
            });

        },
        funObj:function () {
            var $this = this;
            return {
                addProdAction : function () {
                    var dom = $($this.demoModel.clone());
                    $('.modelTr:last').after(dom);
                    var index = $('.modelTr').length - 1 ;
                    dom.find('[name="prods[0][iProdId]"]').attr('name','prods['+index+'][iProdId]');
                    dom.find('[name="prods[0][iPLId]"]').attr('name','prods['+index+'][iPLId]');
                    dom.find('[name="prods[0][iNum]"]').attr('name','prods['+index+'][iNum]');
                    dom.find('[name="prods[0][iStatus]"]').attr('name','prods['+index+'][iStatus]');
                    dom.find('[name="prods['+index+'][iProdId]"]').select2();
                    dom.find('[id="prodId_0"]').attr('id','prodId_'+index);
                },
                submitForm:function () {
                    $this.form.submit('lm/receive/saveReturn',{existPhoneNum: $this.existPhone},function (res) {
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        if(xhExtra.checkError(res)){
                            return false;
                        }
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.closeModal();
                        window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                    },{check:false})
                },
                changeProd: function (_id) {
                    var prod = $('#xh_form').find('#prodId_'+_id).find("option:selected").text();
                    var display = 'none';
                    if (prod === '手机卡') {
                        display = 'contents';
                        if ($.inArray(_id, $this.existPhone) === -1) {//没有
                            $this.existPhone.push(_id);
                        }
                    } else {//包括取消手机卡和选择其他
                        if ($.inArray(_id, $this.existPhone) !== -1) {//取消手机卡
                            $this.existPhone.splice($.inArray(_id,$this.existPhone), 1);//删除元素
                        }
                        if (JSON.stringify($this.existPhone) === '[]'){
                            display = 'none';
                        } else {
                            display = 'contents';
                        }
                    }
                    if (display === 'none') {
                        $('#xh_form').find('#phoneNum').val('');
                        $('#phoneNumTr').hide();
                    } else {
                        $('#phoneNumTr').show();
                    }
                    // document.getElementById('phoneNumTr').style.display = display;
                }
            }
        }
    }
};

/**
 * Date扩展
 * @param format
 * @returns {*}
 */
Date.prototype.format =function(format)
{
    var o = {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(), //day
        "h+" : this.getHours(), //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
        (this.getFullYear()+"").substr(4- RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length==1? o[k] :
                ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
};
